import 'package:flutter/material.dart';

enum PanelState { open, center, close, exit }

class SliverPanelController extends ChangeNotifier {
  late PanelState initPanelState = PanelState.close;

  SliverPanelController({this.initPanelState = PanelState.center});

  PanelState get data => initPanelState;

  set data(PanelState state) {
    initPanelState = state;
    notifyListeners();
  }
}

class SlidingPanelView extends StatefulWidget {
  final double heightOpen; //展开高度
  final double heightCenter; //中间高度
  final double heightClose; //闭合高度
  final Widget headWidget; //标题布局（内含滑动操作）
  final Widget bodyWidget; //内容布局
  final Color backColor; //背景色
  final SliverPanelController controller;

  SlidingPanelView({
    super.key,
    this.heightOpen = 600,
    this.heightCenter = 360,
    this.heightClose = 100,
    required this.headWidget,
    required this.bodyWidget,
    this.backColor = Colors.transparent,
    SliverPanelController? controller,
  }) : controller = controller ?? SliverPanelController();

  @override
  State<StatefulWidget> createState() {
    return _SlidingPanelView();
  }
}

class _SlidingPanelView extends State<SlidingPanelView> {
  final ValueNotifier<PanelState> _panelState = ValueNotifier(PanelState.close);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _panelState.value = widget.controller.data;
    widget.controller.addListener(() {
      _panelState.value = widget.controller.data;
    });
  }

  double panelHeight() {
    if (_panelState.value == PanelState.open) {
      return widget.heightOpen;
    } else if (_panelState.value == PanelState.center) {
      return widget.heightCenter;
    } else if (_panelState.value == PanelState.close) {
      return widget.heightClose;
    } else {
      return 0;
    }
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: _panelState,
      // valueListenable: _panelState,
      builder: (context, value, child) {
        return AnimatedContainer(
          color: widget.backColor,
          duration: const Duration(milliseconds: 200),
          width: double.infinity,
          height: panelHeight(),
          child: Column(
            children: [
              headView(),
              Expanded(child: widget.bodyWidget),
            ],
          ),
        );
      },
    );
  }

  double pointerMove = 0;
  bool isCan = true;

  Widget headView() {
    return Listener(
      //手指按下
      onPointerDown: (event) {
        pointerMove = event.position.dy;
        isCan = true;
      },
      onPointerMove: (event) {
        if (isCan) {
          //手指下滑触发
          if (event.position.dy - pointerMove > 20) {
            isCan = false;
            if (_panelState.value == PanelState.open) {
              _panelState.value = PanelState.center;
            } else if (_panelState.value == PanelState.center) {
              _panelState.value = PanelState.close;
            }
          } else if (event.position.dy - pointerMove < -20) {
            //手指上滑触发
            isCan = false;
            if (_panelState.value == PanelState.close) {
              _panelState.value = PanelState.center;
            } else if (_panelState.value == PanelState.center) {
              _panelState.value = PanelState.open;
            }
          }
        }
      },
      onPointerUp: (event) {
        isCan = true;
      },
      child: widget.headWidget,
    );
  }

  @override
  void dispose() {
    super.dispose();
    _panelState.dispose();
  }
}
